<template>
  <div class="app-container">
    <el-row :gutter="20">
      <!--乡审批机构数据-->
      <el-col :span="24" :xs="24">
        <el-form :model="queryParams" ref="queryForm" :inline="true" label-width="68px">
          <el-form-item label-width="160" label="乡审批机构名称" prop="UnitName">
            <el-input
              v-model="queryParams.unitName"
              placeholder="请输入乡审批机构名称"
              clearable
              size="small"
              style="width: 240px"
              @keyup.enter.native="handleQuery"
            />
          </el-form-item>
          <el-form-item>
            <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
            <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
          </el-form-item>
        </el-form>

        <el-row :gutter="10" class="mb8">
          <el-col :span="1.5">
            <el-button
              type="primary"
              icon="el-icon-plus"
              size="mini"
              @click="handleAdd"
              v-hasPermi="['county:unit:add']"
            >新增</el-button>
          </el-col>
          <el-col :span="1.5">
            <el-button
              type="success"
              icon="el-icon-edit"
              size="mini"
              :disabled="single"
              @click="handleUpdate"
              v-hasPermi="['county:unit:edit']"
            >修改</el-button>
          </el-col>
          <el-col :span="1.5">
            <el-button
              type="danger"
              icon="el-icon-delete"
              size="mini"
              :disabled="multiple"
              @click="handleDelete"
              v-hasPermi="['county:unit:remove']"
            >删除</el-button>
          </el-col>
          <el-col :span="1.5">
            <el-button
              type="info"
              icon="el-icon-upload2"
              size="mini"
              @click="handleImport"
              v-hasPermi="['county:unit:import']"
            >导入</el-button>
          </el-col>
          <el-col :span="1.5">
            <el-button
              type="warning"
              icon="el-icon-download"
              size="mini"
              @click="handleExport"
              v-hasPermi="['county:unit:export']"
            >导出</el-button>
          </el-col>
        </el-row>

        <el-table v-loading="loading" :data="unitList" @selection-change="handleSelectionChange">
          <el-table-column type="selection" width="50" align="center" />
          <el-table-column label="乡审批机构编号" align="center" prop="unitId" />
          <el-table-column label="乡审批机构名称" align="center" prop="unitName" :show-overflow-tooltip="true" />
          <el-table-column label="乡审批机构签章" align="center" prop="unitSignature" :show-overflow-tooltip="true" >
            <template slot-scope="scope">
              <img style="margin-left: 0; width: 180px; height: 180px" :src="'http://localhost:8080' + scope.row.unitSignature" alt="">
            </template>
          </el-table-column>
          <el-table-column
            label="操作"
            align="center"
            width="180"
            class-name="small-padding fixed-width"
          >
            <template slot-scope="scope">
              <el-button
                size="small"
                type="text"
                icon="el-icon-edit"
                @click="handleUpdate(scope.row)"
                v-hasPermi="['county:unit:edit']"
              >修改</el-button>
              <el-button
                size="small"
                type="text"
                icon="el-icon-delete"
                @click="handleDelete(scope.row)"
                v-hasPermi="['county:unit:remove']"
              >删除</el-button>
            </template>
          </el-table-column>
        </el-table>

        <pagination
          v-show="total>0"
          :total="total"
          :page.sync="queryParams.pageNum"
          :limit.sync="queryParams.pageSize"
          @pagination="getList"
        />
      </el-col>
    </el-row>

    <!-- 添加或修改参数配置对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="600px" append-to-body>
      <el-form style="margin-left: 60px" ref="form" :model="form" :rules="rules" label-width="180">
        <el-row>
          <el-col :span="12">
            <el-form-item label="乡审批机构名称" prop="unitName">
              <el-input v-model="form.unitName" placeholder="请输入乡审批机构名称" />
            </el-form-item>
            <el-form-item label="定制签章" prop="unitSignature">
              <el-input v-model="centerName" placeholder="请输入出自名称"></el-input>
              <div class="block">
                <el-date-picker
                  style="margin-top: 20px; width: 200px"
                  v-model="year"
                  type="date"
                  value-format="yyyy年MM月dd日"
                  format="yyyy 年 MM 月 dd 日"
                  placeholder="选择日期">
                </el-date-picker>
              </div>
              <el-button v-show="title.includes('修改')" style="margin-top: 20px" type="primary" @click="generateGraphic">生成签章</el-button>
            </el-form-item>
            <img width="100%" style="margin-left: 130px" :src="'http://localhost:8080' + path" alt="">
          </el-col>
        </el-row>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>

  </div>
</template>

<script>
  import { listData, getData, delData, addData, updateData, exportData, } from "@/api/manager/unit";
  import "@riophae/vue-treeselect/dist/vue-treeselect.css";
  import {generateGraphic} from "../../../api/manager/unit";

  export default {
    name: "Unit",
    data() {
      return {
        year: '',
        centerName: '',
        currentId: '',
        path: '',
        // 遮罩层
        loading: true,
        // 选中数组
        ids: [],
        // 非单个禁用
        single: true,
        // 非多个禁用
        multiple: true,
        // 总条数
        total: 0,
        // 乡审批机构表格数据
        unitList: null,
        // 弹出层标题
        title: "",
        // 部门树选项
        // 是否显示弹出层
        open: false,
        // 状态数据字典
        statusOptions: [],
        // 表单参数
        form: {},
        // 乡审批机构导入参数
        upload: {
          // 是否显示弹出层（乡审批机构导入）
          open: false,
          // 弹出层标题（乡审批机构导入）
          title: "",
          // 是否禁用上传
          isUploading: false,
          // 是否更新已经存在的乡审批机构数据
          updateSupport: 0,
          // 设置上传的请求头部
          // 上传的地址
          url: process.env.VUE_APP_BASE_API + "/county/unit/importData"
        },
        // 查询参数
        queryParams: {
          pageNum: 1,
          pageSize: 10,
          unitName: undefined,
        },
        // 表单校验
        rules: {

        }
      };
    },
    created() {
      this.getList();
    },
    methods: {
      generateGraphic() {
        let formData = new FormData();
        formData.append("unitId", this.currentId)
        formData.append("message", '位' + this.unitName)
        formData.append("centerName", this.centerName)
        formData.append("year", this.year)
        generateGraphic(formData).then(res => {
          if (res) {
            this.msgSuccess('生成成功!')
            console.log(res)
            this.getList()
            this.form.unitSignature = res.imagFilePath
            this.path = res.imagFilePath
            this.centerName = ''
            this.year = ''
            this.form.unitName = ''
          }
        })
      },
      /** 查询乡审批机构列表 */
      getList() {
        this.loading = true;
        listData(this.addDateRange(this.queryParams)).then(response => {
          console.log(response)
            this.unitList = response.rows;
            this.total = response.total;
            this.loading = false;
          }
        );
      },
      // 取消按钮
      cancel() {
        this.open = false;
        this.reset();
      },
      // 表单重置
      reset() {
        this.form = {
          unitId: undefined,
          unitName: undefined,
          unitSignature: undefined
        };
        this.resetForm("form");
      },
      /** 搜索按钮操作 */
      handleQuery() {
        this.queryParams.page = 1;
        this.getList();
      },
      /** 重置按钮操作 */
      resetQuery() {
        this.dateRange = [];
        this.resetForm("queryForm");
        this.handleQuery();
      },
      // 多选框选中数据
      handleSelectionChange(selection) {
        this.ids = selection.map(item => item.unitId);
        this.single = selection.length != 1;
        this.multiple = !selection.length;
      },
      /** 新增按钮操作 */
      handleAdd() {
        this.reset();
        this.open = true
        this.title = "添加乡审批机构"
        this.path = ''
      },
      /** 修改按钮操作 */
      handleUpdate(row) {
        this.reset();
        this.path = row.unitSignature
        this.unitName = row.unitName
        this.currentId = row.unitId
        const unitId = row.unitId || this.ids;
        getData(unitId).then(response => {
          this.form = response.data;
          this.open = true;
          this.title = "修改乡审批机构";
        });
      },
      /** 提交按钮 */
      submitForm: function() {
        this.$refs["form"].validate(valid => {
          if (valid) {
            if (this.form.unitId != undefined) {
              updateData(this.form).then(response => {
                if (response.code === 200) {
                  this.msgSuccess("修改成功");
                  this.open = false;
                  this.getList();
                }
              });
            } else {
              addData(this.form).then(response => {
                if (response.code === 200) {
                  this.msgSuccess("新增成功");
                  this.open = false;
                  this.getList();
                }
              });
            }
          }
        });
      },
      /** 删除按钮操作 */
      handleDelete(row) {
        const unitIds = row.unitId || this.ids;
        this.$confirm('是否确认删除乡审批机构编号为"' + unitIds + '"的数据项?', "警告", {
          confirmButtonText: "确定",
          cancelButtonText: "取消",
          type: "warning"
        }).then(function() {
          return delData(unitIds);
        }).then(() => {
          this.getList();
          this.msgSuccess("删除成功");
        }).catch(function() {});
      },
      /** 导出按钮操作 */
      handleExport() {
        const queryParams = this.queryParams;
        this.$confirm('是否确认导出所有乡审批机构数据项?', "警告", {
          confirmButtonText: "确定",
          cancelButtonText: "取消",
          type: "warning"
        }).then(function() {
          return exportData(queryParams);
        }).then(response => {
          this.download(response.msg);
        }).catch(function() {});
      },
      /** 导入按钮操作 */
      handleImport() {
        this.upload.title = "乡审批机构导入";
        this.upload.open = true;
      },
    }
  };
</script>
